package us.mooresr.geartrack.server.handler;

import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;

import us.mooresr.geartrack.server.DAO;
import us.mooresr.geartrack.shared.action.PutItemAction;
import us.mooresr.geartrack.shared.action.PutItemResult;
import us.mooresr.geartrack.shared.model.Item;

import com.google.inject.Inject;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.shared.ActionException;

public class PutItemActionHandler implements
		ActionHandler<PutItemAction, PutItemResult> {

	@Inject
	public PutItemActionHandler() {
	}

	@Override
	public PutItemResult execute(PutItemAction action, ExecutionContext context)
			throws ActionException {
		DAO dao = new DAO();
		
		Item newItem = action.getItemIn();
		
		dao.ofy().put(newItem);
		
		PutItemResult result = new PutItemResult(newItem);
		
		return result;
	}

	@Override
	public void undo(PutItemAction action, PutItemResult result,
			ExecutionContext context) throws ActionException {
	}

	@Override
	public Class<PutItemAction> getActionType() {
		return PutItemAction.class;
	}
}
